package com.example.hobby;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.fresh.R;

import java.util.ArrayList;
import java.util.List;

public class Fragment_hobby extends Fragment implements View.OnClickListener {
    private TextView item_detail, item_category_report;
    private ViewPager vp;
    private Fragment_hobby_one fragmentHobbyone;
    private Fragment_hobby_two fragmentHobbytwo;
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private FragmentAdapter mFragmentAdapter;
    private PieChartView mPieChartView;

    public Fragment_hobby() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_hobby, container, false);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initViews();
        dialogShow();
        mFragmentAdapter = new FragmentAdapter(this.getChildFragmentManager(), mFragmentList);
        vp.setOffscreenPageLimit(2);//ViewPager的缓存为2帧
        vp.setAdapter(mFragmentAdapter);
        vp.setCurrentItem(0);//初始设置ViewPager选中第一帧
        item_detail.setTextColor(Color.parseColor("#1ba0e1"));

        //ViewPager的监听事件
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                /*此方法在页面被选中时调用*/
                changeTextColor(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                /*此方法是在状态改变的时候调用，其中arg0这个参数有三种状态（0，1，2）。
                arg0==1的时辰默示正在滑动，
                arg0==2的时辰默示滑动完毕了，
                arg0==0的时辰默示什么都没做。*/
            }
        });
    }

    /**
     * 初始化布局View
     */
    private void initViews() {
        item_detail = getView().findViewById(R.id.item_detail);
        item_category_report = getView().findViewById(R.id.item_category_report);

        item_detail.setOnClickListener(this);
        item_category_report.setOnClickListener(this);

        vp = getView().findViewById(R.id.mainViewPager);
        fragmentHobbyone = new Fragment_hobby_one();
        fragmentHobbytwo = new Fragment_hobby_two();
        //给FragmentList添加数据
        mFragmentList.add(fragmentHobbyone);
        mFragmentList.add(fragmentHobbytwo);

        float[] data = new float[]{60f, 40f, 30f,28f, 11f};
        String[] name = new String[]{"食品", "零食", "生活用品","其它","化妆品"};
        mPieChartView = getView().findViewById(R.id.percentPieView);
        mPieChartView.setData(data, name);
        mPieChartView.startAnimation(2000);

    }

    public static class FragmentAdapter extends FragmentPagerAdapter {
        List<Fragment> fragmentList;
        public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    /**
     * 点击头部Text 动态修改ViewPager的内容
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_detail:
                vp.setCurrentItem(0, true);
                break;
            case R.id.item_category_report:
                vp.setCurrentItem(1, true);
                break;
        }
    }


    /**
     * 由ViewPager的滑动修改头部导航Text的颜色
     * @param position
     */
    private void changeTextColor(int position) {
        if (position == 0) {
            item_detail.setTextColor(Color.parseColor("#1ba0e1"));
            item_category_report.setTextColor(Color.parseColor("#000000"));
        } else if (position == 1) {
            item_category_report.setTextColor(Color.parseColor("#1ba0e1"));
            item_detail.setTextColor(Color.parseColor("#000000"));
        }
    }

    //进入爱好分析时弹出的爱好分析提示
    private void dialogShow(){
        AlertDialog texts = new AlertDialog.Builder(getActivity())
                .setTitle("爱好分析:")
                .setMessage("在11月里，MrFresh陪伴了你20天\n" +
                        "你最喜欢的物品类型是：" +"\n"+
                        "食品\n" +
                        "添加在MrFresh最多的TOP3是：\n" +
                        "面包"+"(10次)\n"+
                        "火腿肠"+"(7次)\n"+
                        "鱼罐头"+"(3次)\n")
                .create();
        texts.show();
    }
}
